package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.TrainingGroup;
import ModelLayer.TrainingCategory;

public class DBTrainingGroup implements DBTrainingGroupIF
{
	private Connection con;
    
    public DBTrainingGroup() 
    {
    	con = DBConnection.getInstance().getDBcon();
    }
	
    public ArrayList<TrainingGroup> getAllTrainingGroup(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public TrainingGroup searchTrainingGroupID(int trainingGroupID, boolean retriveAssociation)
    {   
    	String wClause = "TrainingGroupID = " + trainingGroupID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    public TrainingGroup searchTrainingGroupName(String name, boolean retriveAssociation)
    {
        String wClause = "Name  = " + name + "%'";
        System.out.println("Search TrainingGroup " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public int insertTrainingGroup(TrainingGroup tg)
    {
    	int nextId = GetMax.getMaxId("Select max(id) from TrainingGroup");
        nextId = nextId + 1;
        System.out.println("next id = " +  nextId);
    	
       int rc = -1;
	   String query="INSERT INTO TrainingGroup(TrainingGroupID, Name, TrainingCategoryID) VALUES (" + nextId + ",'" + tg.getName() + "','" + tg.getTrainingCategory().getTrainingCategoryID() + "'";
                     
      System.out.println("insert : " + query);		  					
      try{
            Statement stmt = con.createStatement();
	 	  	stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
		}	
	 	catch(Exception ex){
	 	  	System.out.println("Insert exception in TrainingGroup db: "+ex);
	   }
	   return(rc);
	}
	
	public int updateTrainingGroup(TrainingGroup tg)
	{
		TrainingGroup tgObj = tg;
		int rc=-1;
	  
		String query="UPDATE TrainingGroup SET " +
			" WHERE TrainingGroupID = '" + tgObj.getTrainingGroupID() + "'" +
		 	"Name ='" + tgObj.getName() + "'" +
		 	"TrainingCategoryID ='" + tgObj.getTrainingCategory().getTrainingCategoryID() + "'";
			
            System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);
	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in TrainingGroup db: "+ex);
	  	}
		return(rc);
	}
	
	public int delete(int id)
	{
               int rc=-1;
	  
	  	String query="DELETE FROM TrainingGroup WHERE id = " +
				id + "";
                System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in TrainingGroup db: "+ex);
   	    }
		return(rc);
	}
	
	private ArrayList<TrainingGroup> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<TrainingGroup> trainingGroupList = new ArrayList<TrainingGroup>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				TrainingGroup tgObj = new TrainingGroup();
				tgObj = buildTrainingGroup(results);	
				trainingGroupList.add(tgObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return trainingGroupList;
	}
	
	private TrainingGroup singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		TrainingGroup tgObj = new TrainingGroup();
	    String query =  buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			tgObj = buildTrainingGroup(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return tgObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM TrainingGroup";
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;		
		return query;
	}

	private TrainingGroup buildTrainingGroup(ResultSet results)
      {   
		TrainingGroup tgObj = new TrainingGroup();
          try
          {
                tgObj.setTrainingGroupID(results.getInt("TrainingGroupID"));
                tgObj.setName(results.getString("Name"));
                tgObj.setTrainingCategory(new TrainingCategory(results.getInt("TrainingCategoryID")));
          }
         catch(Exception e)
         {
             System.out.println("error in building the TrainingGroup object");
         }
         return tgObj;
      }

}
